export default {
    namespaced: true,
    state: () => ({
      items: [],
      total: 100,  
      allChecked: true
    }),
    mutations: {
      calc_total (state) {
        let total = 0
        const checkItems = state.items.filter(item => item.checked)
        checkItems.forEach(item => {
          total += item.num * item.minPrice 
        })
        state.total = parseFloat(total.toFixed(2))
      },
      updata_items (state, items) {
        state.items = items
      },
      set_single_checked (state, {index, checked}) {
        state.items[index].checked = checked
      },
      set_allChecked (state, checked) {
        state.allChecked = checked
      },
      set_all_single_checked (state, checked) {
        state.items.forEach(item => { item.checked = checked })
      },
      del_item (state, index) {
        state.items.splice(index, 1)
      },
      add_cart (state, item) {
        state.items.unshift(item)
      }
    }
}